package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.infrastructure.bean.OfflineOrdersBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.OfflineOrdersMapper;
import com.molichuxing.services.infrastructure.dto.request.create.OfflineOrdersCreateDto;
import com.molichuxing.services.infrastructure.dto.response.OfflineOrdersDto;
import com.molichuxing.services.infrastructure.service.OfflineOrdersService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 线下支付订单排期 服务实现类
 * </p>
 *
 * @author zhaopenghui
 * @since 2019-08-21
 */
@Service("offlineOrdersService")
public class OfflineOrdersServiceImpl implements OfflineOrdersService {

    @Resource
    private OfflineOrdersMapper offlineOrdersMapper;


    /**
     * 添加线下支付
     *
     * @param offlineOrdersList
     * @return
     * @throws Exception
     */
    @Override
    public Boolean createOfflineOrders(List<OfflineOrdersCreateDto> offlineOrdersList) throws Exception {
        Assert.notNull(offlineOrdersList, "添加失败");

        List<OfflineOrdersBean> offlineOrdersBeanList = Convert.toOfflineOrdersBeanList(offlineOrdersList);
        int batchResult = offlineOrdersMapper.createBatch(offlineOrdersBeanList);
        if (batchResult <= 0) {
            return false;
        }
        return true;
    }

    /**
     * 获取线下支付id
     *
     * @param orderCode
     * @param scheduleId
     * @return
     * @throws Exception
     */
    @Override
    public Integer getOfflineId(Long orderCode, Integer scheduleId) throws Exception {
        if (null == orderCode || null == scheduleId) {
            return null;
        }

        OfflineOrdersBean offlineOrdersBean = offlineOrdersMapper.getOffline(null, orderCode, scheduleId);
        if (null == offlineOrdersBean) {
            return null;
        }
        return offlineOrdersBean.getOfflineId();
    }

    /**
     * 线下支付订单排期
     *
     * @param offlineId
     * @return
     * @throws Exception
     */
    @Override
    public OfflineOrdersDto getOfflineOrders(Integer offlineId) throws Exception {
        if (null == offlineId) {
            return null;
        }

        OfflineOrdersBean offlineOrdersBean = offlineOrdersMapper.getOffline(offlineId, null, null);
        return Convert.toOfflineOrdersDto(offlineOrdersBean);
    }

    /**
     * 获取订单下所有支付id
     *
     * @param orderCode
     * @return
     * @throws Exception
     */
    @Override
    public List<Integer> getOfflineIdList(Long orderCode) throws Exception {
        return offlineOrdersMapper.getOfflineIdList(orderCode);
    }

    /**
     * 通过线下支付id获取排期id集合
     *
     * @param offlineId
     * @return
     */
    @Override
    public List<Integer> getScheduleIdList(Integer offlineId) {
        return offlineOrdersMapper.getScheduleIdList(offlineId);
    }

    /**
     * 线下支付排期集合
     *
     * @param offlineId
     * @return
     */
    @Override
    public List<OfflineOrdersDto> getOfflineOrdersList(Integer offlineId) {
        List<OfflineOrdersBean> offlineOrdersList = offlineOrdersMapper.getOfflineOrdersList(offlineId);
        return Convert.toOfflineOrdersList(offlineOrdersList);
    }


}
